@page "/project_group/update/{id:int}"
@using FOPS.Abstract.MetaInfo.Entity
@using FOPS.Abstract.MetaInfo.Server
@using FOPS.Abstract.K8S.Entity
@inject IIocManager _iocManager;
@inject NavigationManager nav;
@inject IJSRuntime js;

<div class="layui-card">
    <div class="layui-card-header">
        编辑项目组
        <div class="layui-right">
            <button class="layui-btn" @onclick="GotoList"><i class="am-icon-plus"></i> 列表</button>
        </div>
    </div>
    <div class="layui-card-body" style="padding: 15px;">
        <div class="layui-form-item">
            <label class="layui-form-label">ID</label>
            <div class="layui-input-inline">
                <label class="layui-form-label" style="text-align: left">@_info.Id</label>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">集群</label>
            <div class="layui-input-block">
                @foreach (var clusterVO in _lstCluster)
                {
                    <div class="layui-unselect layui-form-checkbox layui-form-checked" lay-skin="primary" style="cursor: auto;">
                        <input type="checkbox" @bind="_selectCluster[clusterVO.Id]"/>
                        <span>【@clusterVO.RuntimeEnvType.ToString()】@clusterVO.Name</span>
                    </div>
                }
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">项目名称</label>
            <div class="layui-input-block">
                <input type="text" @bind="_info.Name" autocomplete="off" placeholder="请输入项目名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-footer" style="left: 0;">
                    <button class="layui-btn" @onclick="UpdateInfo">保存</button>
                    <button class="layui-btn layui-btn-danger" @onclick="Delete">删除</button>
                </div>
            </div>
        </div>
    </div>
</div>

@code{

    [Parameter]
    public int Id { get; set; }

    private ProjectGroupVO _info = new()
    {
    };
    private List<ClusterVO> _lstCluster = new();
    private Dictionary<int, bool> _selectCluster = new();

    private async Task UpdateInfo()
    {
        if (Id < 1 || _info == null)
        {
            await js.InvokeVoidAsync("layer.alert", "项目组不存在", new
            {
                icon = 2,
                title = "出错了"
            });
            GotoList();
            return;
        }

        try
        {
            _info.ClusterIds = _selectCluster.Where(o => o.Value).Select(o => o.Key).ToList();

            if (string.IsNullOrWhiteSpace(_info.Name) || _info.ClusterIds == null || _info.ClusterIds.Count == 0)
            {
                throw new Exception("项目组、集群必须填写.");
            }
            await _iocManager.Resolve<IProjectGroupService>().UpdateAsync(Id, _info);
            await js.InvokeVoidAsync("layer.msg", "修改成功", new
            {
                icon = 1
            });
            GotoList();
        }
        catch (Exception e)
        {
            await js.InvokeVoidAsync("layer.alert", e.Message, new
            {
                icon = 2,
                title = "出错了"
            });
        }
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (Id < 1 || _info == null)
        {
            await js.InvokeVoidAsync("layer.alert", "项目组不存在", new
            {
                icon = 2,
                title = "出错了"
            });
            GotoList();
        }
    }

    protected override async Task OnParametersSetAsync()
    {
        if (Id > 0 && _info.Id == 0)
        {
            _info = await _iocManager.Resolve<IProjectGroupService>().ToInfoAsync(Id);
            if (_info == null)
            {
                await js.InvokeVoidAsync("layer.alert", "项目组不存在", new
                {
                    icon = 2,
                    title = "出错了"
                });
                GotoList();
            }

            _lstCluster = await _iocManager.Resolve<IClusterService>().ToListAsync() ?? new();
            _selectCluster = _lstCluster.ToDictionary(o => o.Id, o => false);

            foreach (var infoClusterId in _info.ClusterIds)
            {
                _selectCluster[infoClusterId] = true;
            }
        }
    }

    private void GotoList()
    {
        nav.NavigateTo("/project_group/list");
    }

    private async Task Delete()
    {
        var count = await _iocManager.Resolve<IProjectService>().GroupCountAsync(Id);
        if (count > 0)
        {
            await js.InvokeVoidAsync("layer.alert", $"删除项目组前，需先删除该组的{count}个项目", new
            {
                icon = 2,
                title = "出错了"
            });
            nav.NavigateTo("/project/list");
            return;
        }
        await _iocManager.Resolve<IProjectGroupService>().DeleteAsync(Id);
        GotoList();
    }
}